export default {
// 全屏
  handleFullScreen(dom, cb) {
    this.cb = cb
    let mapMainContent;
    // 判断是否有dom传入，没有则是整个页面全屏展示
    dom ? mapMainContent = document.querySelector(dom) : mapMainContent = document.documentElement;
    if (this.isFUllScreenEnabled()) {
      if (this.hasFullScreenElement()) {
        this.exitFullScreen();
      } else {
        this.setFullScreen(mapMainContent);
      }
      this.fn = this.listener.bind(this)

      document.addEventListener('fullscreenchange', this.fn)
    } else {
      console.log('此浏览器不支持全屏');
    }
  },
  listener() {
    // 监听到屏幕变化，在回调中判断是否已退出全屏 如果已退出全屏 把本地状态修改为false
    // document.fullscreen 会返回true或false
    const isFull = document.fullscreen
    this.cb && this.cb(isFull)
    if (!isFull) {
      document.removeEventListener('fullscreenchange', this.fn)
    }
  },
// 判断浏览器是否支持全屏
  isFUllScreenEnabled() {
    return document.fullscreenEnabled || document.webkitFullscreenEnabled || document.mozFullScreenEnabled || document.msFullscreenEnabled;
  },
// 判断是否有已全屏的元素
  hasFullScreenElement() {
    return document.fullscreenElement || document.webkitFullscreenElement || document.mozFullScreenElement || document.msFullscreenElement;
  },
// 将目标元素设置为全屏展示
  setFullScreen(target) {
    if (target.requestFullscreen) {
      target.requestFullscreen();
    }
    if (target.webkitRequestFullscreen) {
      target.webkitRequestFullscreen();
    }
    if (target.mozRequestFullScreen) {
      target.mozRequestFullScreen();
    }
    if (target.msRequestFullscreen) {
      target.msRequestFullscreen();
    }
  },
// 文档退出全屏
  exitFullScreen() {
    if (document.exitFullscreen) {
      document.exitFullscreen();
    }
    if (document.webkitExitFullscreen) {
      document.webkitExitFullscreen();
    }
    if (document.mozCancelFullScreen) {
      document.mozCancelFullScreen();
    }
    if (document.msExitFullscreen) {
      document.msExitFullscreen();
    }

  }
}
